baseline = [-2000 -1000];
conditions = [1 3]; %1=audio; 2=blank; 3=light

% load study
% ----------
eeglab
[STUDY ALLEEG]  = pop_loadstudy('filename', 'nondual27subjects.study', 'filepath', pwd);
[STUDY, ALLEEG] = std_checkset(STUDY, ALLEEG);
CURRENTSTUDY    = 1; EEG = ALLEEG; CURRENTSET = [1:length(EEG)];
STUDY.changrp   = [];
[STUDY ALLEEG] = std_precomp(STUDY, ALLEEG, {}, 'allcomps', 'on');
[STUDY ALLEEG] = std_precomp(STUDY, ALLEEG, {}, 'allcomps', 'on', 'recompute', 'on', 'erp', 'on', 'rmbase', baseline );
[STUDY ALLEEG] = std_precomp(STUDY, ALLEEG, {}, 'allcomps', 'on', 'recompute', 'on', 'ersp', 'on', 'erspparams', ...
    { 'cycles', 0, 'nfreqs', 50, 'baseline', -1000, 'timesout', 100, 'freqs', [2 50], 'trialbase', 'on', 'savetrials', 'on' } );
%[STUDY ALLEEG] = std_precomp(STUDY, ALLEEG, {}, 'allcomps', 'on', 'recompute', 'on', 'spec', 'on', 'specparams', { 'timerange' [-1010 -10] 'specmode' 'fft' } );
return

% get all subjects data
% ----------------
ff = {'elaine' ; 'edmay' ; 'bryan' ; 'randy' ; 'gene' ; 'hoss' ; 'gordon' ; 'joe' ; 'curt'; 'johnny'; 'sherri'; ...
      'richard'; 'judy' ; 'johnastin' ; 'john'; 'federico' ; 'steve' ; 'christer' ; 'carol' ; 'francis' ; 'swamisita' ; ...
      'yogafarm'; 'loch' ; 'gary' ; 'zoran' ; 'james'; 'arno' };
ff = { 'federico' };
    
for subject = 1:length(ff)
    chans = 1:ALLEEG(1).nbchan; chans(21:22) = []; % selection of channels
    STUDY = pop_erpparams(STUDY,  'ylim',[-5 5], 'timerange',[-500 500], 'topotime',[], 'condstats', 'off', 'groupstats', 'off');
    [STUDY alldata alltimes] = std_readerp(STUDY,ALLEEG,'channels', { ALLEEG(1).chanlocs.labels }, 'timerange', [-500 500], 'singletrials', 'on', 'subject', ff{subject});

    STUDY = std_erpplot(STUDY,ALLEEG,'channels', { ALLEEG(1).chanlocs.labels }); % load data
    %'cycles', [3 0.8], 'nfreqs', 50, 'baseline', -1000, 'freqs', [1.5 50], 'timesout', 'trialbase', 'on'

    % get data into a large array
    % ---------------------------
    alldata = { [] [] [] };
    for index = 1:length(chans)
        for c = 1:length(STUDY.condition)
            alldata{c}(:,:,index) = STUDY.changrp(chans(index)).erpdata{c};
        end;
    end;
    for c = 1:length(STUDY.condition)
        alldata{c} = permute(alldata{c}, [1 3 2]);
    end;

    % compute statistics and plot new movie
    % -------------------------------------
    threshold = 0.01;
    titlep    = sprintf('p<%1.2f (bootstrap with FDR)', threshold);
    [pcond] = std_stat(alldata(conditions)', 'condstats', 'on', 'statistics', 'bootstrap', 'naccu', 200, 'threshold', threshold, 'mcorrect', 'fdr');
    clear M tmpdata;
    latencies = STUDY.changrp(1).erptimes;
    for index = 1:length(latencies)
        tmppcond{1} = squeeze(pcond{1}(index,:));
        for c = 1:length(conditions)
            tmpdata{c}  = squeeze(mean(alldata{conditions(c)}(index,:,:),3));
        end;
        textstr = sprintf('%2.0f ms', latencies(index));
        std_chantopo(tmpdata', 'threshold', threshold, 'datatype', 'erp', 'unitx', '\muV', 'binarypval', 'on', 'condstats', tmppcond, ...
                               'chanlocs', ALLEEG(1).chanlocs(chans), 'ylim', [-1.5 1.5], 'titles', { STUDY.condition{conditions} titlep }')
        delete(gca)
        textsc(textstr, 'title');
        drawnow;
        M(index) = getframe(gcf);
        close;
    end;
    movie2avi(M, 'erpmovie_nd.avi', 'fps', 5);
end;
